<template>
  <el-form v-bind="$attrs" @validate="formValidate">
    <el-form-item label="名称" prop="name" :rules="[{ required: true, message: '名称不能为空' }]">
      <el-input v-model="formData.name" placeholder="请输入数据标题，不超过50个字符" />
    </el-form-item>
    <el-form-item label="资源描述" prop="desc">
      <el-input
        v-model="formData.desc"
        type="textarea"
        :rows="1"
        autosize
        @blur="specialFormValidate('desc')"
        @keyup.enter.native="specialFormValidate('desc')"
        placeholder="请输入数据的描述信息，不超过200个字符"
      />
    </el-form-item>
    <el-form-item label="缩放">
      <div class="row-item">
        <el-input
          v-model="formData.attr.scaleData"
          placeholder=""
          @blur="specialFormValidate('scale')"
          @keyup.enter.native="specialFormValidate('scale')"
        />
        <div class="unit">%</div>
      </div>
    </el-form-item>
    <!-- <el-form-item label="模型颜色">
      <el-color-picker
        class="color-picker"
        v-model="formData.attr.color"
        @change="specialFormValidate('color')"
      />
      <div class="row-item-small">
        <el-input
          v-model="formData.attr.color"
          placeholder=""
          @blur="specialFormValidate('color')"
          @keyup.enter.native="specialFormValidate('color')"
        />
      </div>
    </el-form-item> -->
    <el-form-item label="位置">
      <div class="shrink" @click="shrink('position')">
        <SvgIcon v-if="shrinkFlag['position']" icon-class="ic_向上" />
        <SvgIcon v-else icon-class="ic_向下" />
      </div>
      <div class="line"></div>
      <div v-if="shrinkFlag['position']" class="shrink-contain">
        <div class="row-item">
          <div class="title">经度</div>
          <el-input
            v-model="formData.attr.position[0]"
            placeholder=""
            @blur="specialFormValidate('position')"
            @keyup.enter.native="specialFormValidate('position')"
          />
          <div class="unit">°</div>
        </div>
        <div class="row-item">
          <div class="title">纬度</div>
          <el-input
            v-model="formData.attr.position[1]"
            placeholder=""
            @blur="specialFormValidate('position')"
            @keyup.enter.native="specialFormValidate('position')"
          />
          <div class="unit">°</div>
        </div>
        <div class="row-item">
          <div class="title">高度</div>
          <el-input
            v-model="formData.attr.position[2]"
            placeholder=""
            @blur="specialFormValidate('position')"
            @keyup.enter.native="specialFormValidate('position')"
          />
          <div class="unit">米</div>
        </div>
      </div>
    </el-form-item>
    <el-form-item label="平移">
      <div class="shrink" @click="shrink('translation')">
        <SvgIcon v-if="shrinkFlag['translation']" icon-class="ic_向上" />
        <SvgIcon v-else icon-class="ic_向下" />
      </div>
      <div class="line"></div>
      <div v-if="shrinkFlag['translation']" class="shrink-contain">
        <div class="row-item">
          <div class="title">X轴</div>
          <el-input
            v-model="formData.attr.translation[0]"
            placeholder=""
            @blur="specialFormValidate('translation')"
            @keyup.enter.native="specialFormValidate('translation')"
          />
          <div class="unit">米</div>
        </div>
        <div class="row-item">
          <div class="title">Y轴</div>
          <el-input
            v-model="formData.attr.translation[1]"
            placeholder=""
            @blur="specialFormValidate('translation')"
            @keyup.enter.native="specialFormValidate('translation')"
          />
          <div class="unit">米</div>
        </div>
        <div class="row-item">
          <div class="title">Z轴</div>
          <el-input
            v-model="formData.attr.translation[2]"
            placeholder=""
            @blur="specialFormValidate('translation')"
            @keyup.enter.native="specialFormValidate('translation')"
          />
          <div class="unit">米</div>
        </div>
      </div>
    </el-form-item>
    <el-form-item label="旋转">
      <div class="shrink" @click="shrink('rotation')">
        <SvgIcon v-if="shrinkFlag['rotation']" icon-class="ic_向上" />
        <SvgIcon v-else icon-class="ic_向下" />
      </div>
      <div class="line"></div>
      <div v-show="shrinkFlag['rotation']" class="shrink-contain">
        <div class="row-item">
          <div class="title">X轴</div>
          <el-input
            v-model="formData.attr.rotation[0]"
            placeholder=""
            @blur="specialFormValidate('rotation')"
            @keyup.enter.native="specialFormValidate('rotation')"
          />
          <div class="unit">°</div>
        </div>
        <div class="row-item">
          <div class="title">Y轴</div>
          <el-input
            v-model="formData.attr.rotation[1]"
            placeholder=""
            @blur="specialFormValidate('rotation')"
            @keyup.enter.native="specialFormValidate('rotation')"
          />
          <div class="unit">°</div>
        </div>
        <div class="row-item">
          <div class="title">Z轴</div>
          <el-input
            v-model="formData.attr.rotation[2]"
            placeholder=""
            @blur="specialFormValidate('rotation')"
            @keyup.enter.native="specialFormValidate('rotation')"
          />
          <div class="unit">°</div>
        </div>
      </div>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { messageUtil } from '@jl/element-plus/utils';
import { ValidateRule } from '@/views/enum/common';
import { notNumberInRange } from '@/utils';
import { round } from 'lodash';

defineOptions({
  name: '',
});

interface IProps {
  form: any;
}

const props = withDefaults(defineProps<IProps>(), {
  form: {},
});

const formData = ref<any>(null);
const emits = defineEmits(['formValidate']);
const shrinkFlag = reactive<any>({
  position: false,
  translation: false,
  rotation: false,
});

function formValidate(fieldName: string, valid: boolean) {
  emits('formValidate', fieldName, valid);
}

function specialFormValidate(fieldName: string) {
  let valid = true;
  if (['desc'].includes(fieldName)) {
    if (formData.value[fieldName].length > 200) {
      valid = false;
    }
  }
  if (['position', 'translation', 'rotation'].includes(fieldName)) {
    formData.value.attr[fieldName].forEach((list: any, index: number) => {
      if (list === '' || isNaN(list)) {
        valid = false;
      }
      if ('translation' === fieldName) return;
      let min = ValidateRule[fieldName][index].min,
        max = ValidateRule[fieldName][index].max;
      if (min && notNumberInRange(list, min, max)) {
        valid = false;
      }
    });
  } else if (['scale'].includes(fieldName)) {
    if (formData.value.attr.scaleData === '' || isNaN(formData.value.attr.scaleData)) {
      valid = false;
    } else {
      formData.value.attr.scale = formData.value.attr.scaleData / 100;
    }
  }
  if (!valid) {
    messageUtil.warning('校验错误');
  }
  emits('formValidate', fieldName, valid);
}

init();
function init() {
  formData.value = props.form;
  const { cimInstance } = window;
  const layer = props.form;
  const config = cimInstance.api.getCurrentLayerConfig(layer.layerName);
  formData.value.attr.translation = [0, 0, 0];
  if (config?.scale) {
    formData.value.attr.scale = config.scale;
    formData.value.attr.scaleData = config.scale * 100;
  }
  if (config?.translation) {
    formData.value.attr.translation = [0, 0, 0];
  }
  if (config?.rotation) {
    formData.value.attr.rotation = [
      round(config.rotation[0], 2),
      round(config.rotation[1], 2),
      round(config.rotation[2], 2),
    ];
  }

  formData.value.attr.position = [
    config.translation[0],
    config.translation[1],
    round(config.translation[2], 2),
  ];
  formData.value.attr.color = config.color || '';
}

function tofixed(arr: []) {
  return arr.map((v: number) => +v.toFixed(2));
}

function shrink(str: string) {
  shrinkFlag[str] = !shrinkFlag[str];
}
</script>

<style lang="less" scoped>
.el-form {
  .row-item-small {
    width: 100px;
    margin-left: 10px;
    line-height: normal;
    outline: 0;
  }
  &-item {
    .row-item {
      width: 100%;
      display: flex;
      margin-bottom: 10px;
      .title {
        flex: 0 0 40px;
      }
      .el-input {
        flex: 1;
      }
      .unit {
        flex: 0 0 24px;
        text-align: center;
      }
      &:last-child {
        margin-bottom: 0px;
      }
    }
  }

  .shrink {
    position: absolute;
    font-size: 15px;
    top: -34px;
    right: 0;
    width: 30px;
    display: flex;
    justify-content: center;
    height: 30px;
    align-items: center;
    cursor: pointer;
  }
  .line {
    height: 1px;
    background: #1f2329;
    width: calc(100% + 40px);
    margin-left: -20px;
    margin-right: -20px;
    margin-top: 8px;
  }

  .line:not(div:last-of-type) {
    margin-bottom: 10px;
  }
  .shrink-contain {
    width: 100%;
    margin-top: 10px;
  }
}
</style>
